package com.jacken.mars.controller;

import com.jacken.mars.commons.annotation.BaseAnnotationNote;
import com.jacken.mars.commons.exception.WorkException;
import com.jacken.mars.entity.resp.R;
import com.jacken.mars.utils.QiniuUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * @version: 2.4.5
 * @author: wq
 * @time: 2021/2/3 11:39
 */
@RestController
@RequestMapping("admin/file")
@AllArgsConstructor
@Slf4j
public class FileController {

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @BaseAnnotationNote
    private R upload(@RequestParam("file") MultipartFile multipartFile) throws IOException {
        if (multipartFile.isEmpty()) {
            throw new WorkException("请选择文件");
        }
        FileInputStream inputStream = (FileInputStream) multipartFile.getInputStream();
        String key = System.currentTimeMillis() + "";
        String path = QiniuUtils.uploadQNImg(inputStream, key);
        log.info("path===>" + path);
        return R.success(path);
    }
}
